package server;

import java.text.*;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
/**
 * Class for working with HTTP date strigs. Implements three standarts,
 * including RFC822, RFC850 and ANSIC. Please visit
 * http://www.faqs.org/rfcs/rfc1945.html section 3.3 for further details.
 * @author vivis
 */
public class HttpDate {
    // Formats
    public static final int RFC822 = 0;
    public static final String RFC822_s = "EE, dd MMM yyyy hh:mm:ss zzz";
   
    public static final int RFC850 = 1;
    public static final String RFC850_s = "EEEE, dd-MMM-yy hh:mm:ss zzz";
   
    public static final int ANSIC = 2;
    public static final String ANSIC_s = "EE MMM dd hh:mm:ss yyyy";
   
    public static final int DEFAULT = RFC822;
   
    public static final Locale LOCALE = Locale.ENGLISH;       
    public static final String TMZ = "GMT";
    public static final TimeZone tz = TimeZone.getTimeZone(TMZ);
   
    public static DateFormat df =  DateFormat.getDateInstance(DateFormat.LONG, LOCALE);
    // Date
    private int year;
    private int month;
    private int day;
       
    // Time
    private int hour;
    private int minute;
    private int second;
   
    Locale engLocale;
   
    private HttpDate() {
       
    }
   
    /**
     * returns foarmat string given its id
     * @param format format id
     * @return format string
     */
    private static String getFormatString(int format) {
        if (format == RFC822) return (RFC822_s);
        if (format == RFC850) return (RFC850_s);
        if (format == RFC850) return (ANSIC_s);
        return (RFC822_s);       
    }
   
    /**
     * Formats date in specified standart.
     * @param format format id, look into format constants
     * @param d date, being formatted
     * @return formatted string
     */
    
    public static String getFormattedDate(int format, Date d) {
        String fs = getFormatString(format);
        DateFormat sdf = new SimpleDateFormat(fs, LOCALE);
        sdf.setTimeZone(tz);
        return sdf.format(d);
    }
   
    /**
     * Formats String in default way (RFC822)
     * @param d date, being formatted
     * @return formated string
     */
    public static String getFormattedDate(Date d) {
        return getFormattedDate(DEFAULT, d);
    }   
   
    /**
     * Formats current date ant time in default way (RFC822).
     * This is a convenience method.
     * @return
     */
    public static String getFormattedDate() {
        return getFormattedDate(DEFAULT, new Date());
    }       
   
    private static Date parseUsing(int method, String da) throws ParseException{
        String fs = getFormatString(method);
        DateFormat sdf = new SimpleDateFormat(fs, LOCALE);
        sdf.setTimeZone(tz);
        return sdf.parse(da);
    }
   
   
    /**
     * Parses string into date object. String must be in one of three
     * specified formats: RFC822, RFC850 or ANSIC.
     * @param parsedDate String, being parsed
     * @return Parsed date
     * @throws java.text.ParseException If does nto match any of 3 patterns
     */
    public static Date parse(String parsedDate) throws ParseException {       
        try {
          return parseUsing(DEFAULT, parsedDate);
        }
        catch (ParseException pe1) {
            try {
              return parseUsing(RFC850, parsedDate);
            }
            catch (ParseException pe2) {
               return parseUsing(ANSIC, parsedDate);
            }
        }
    }
         
}
